#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "signup.h"
#include <QGraphicsDropShadowEffect>
#include "menu.h"
#include "weather.h"
#include "music.h"
#include "smart_home.h"
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    this->setStyleSheet("background-color:qlineargradient(spread:pad,x1:0,y1:0,x2:1,y2:0,stop:0 rgba(251,102,102,200),stop:1 rgba(20,196,188,210));");
    //标签图片加载
    QPixmap *pix = new QPixmap(":/1.png");
    QSize sz = ui->label->size();
    ui->label->setPixmap(pix->scaled(sz));
    ui->label->setScaledContents(true);
    ui->toolButton_2->setIcon(QIcon(":/x.png"));
    ui->toolButton_2->setIconSize(QSize(32, 32));
    ui->toolButton->setIcon(QIcon(":/suoxiao.png"));
    ui->toolButton->setIconSize(QSize(32, 32));

    setWindowFlag(Qt::FramelessWindowHint,true);//去掉主窗口的边框
    setAttribute(Qt::WA_TranslucentBackground,true);//把主窗口透明
    //初始化数据库
    sqlite_Init();

}

MainWindow::~MainWindow()
{
    delete ui;
}

void sqlite_Init()
{

    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName("user.db");
    if(!db.open())
    {
        qDebug()<<"open error";
    }
    //create excle
    QString createsql=QString("create table if not exists user(id integer primary key autoincrement,"
                        "username ntext unique not NULL,"
                        "password ntext not NULL)");
    QSqlQuery query;
    if(!query.exec(createsql)){
        qDebug()<<"table create error";
    }
    else{
        qDebug()<<"table create success";
    }
}


void MainWindow::on_btn_signin_clicked()
{
    sqlite_Init();
    QString username = ui->lineEdit_username->text();
    QString password = ui->lineEdit_password->text();
    QString sql=QString("select * from user where username='%1' and password='%2'")
            .arg(username).arg(password);
    //创建执行语句对象
    QSqlQuery query(sql);
    //判断执行结果
    if(!query.next())
    {
        qDebug()<<"Login error";
        QMessageBox::information(this,"登录认证","登录失败,账户或者密码错误");

    }
    else
    {
        qDebug()<<"Login success";
        QMessageBox::information(this,"登录认证","登录成功");//接口在这
        menu *w = new menu;
        w->show();

        this->close();
    }
}

void MainWindow::on_btn_signup_clicked()
{
    this->close();
    Signup *s = new Signup;
    s->show();

}

void MainWindow::on_toolButton_2_clicked()
{
    qDebug()<<"槽函数接到了"<<endl;
    close();
}

void MainWindow::on_toolButton_clicked()
{
    qDebug()<<"槽函数接到了"<<endl;
    showMinimized();
}
